/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Records.RecordsMain;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Alexander
 */
public class RecTable {

    static void bottomTable(String song, String theKey, String bpm, String genre, String album, String artist, String location, String Danceability, String Energy, String Acousticness) {
        System.out.println("RecTable.bottomTable(" + song + ", " + theKey + "," + bpm + ", " + genre + "," + album + ", " + artist + "," + location + ", " + Danceability + ", " + Energy + ", " + Acousticness + ")");

        int bpmI = Integer.parseInt(bpm);
        int Max = bpmI + 5;
        int Min = bpmI - 5;
        int DMax = Integer.parseInt(Danceability) + 20;
        int DMin = Integer.parseInt(Danceability) - 20;
        int EMax = Integer.parseInt(Energy) + 15;
        int EMin = Integer.parseInt(Energy) - 15;
        int AMax = Integer.parseInt(Acousticness) + 15;
        int AMin = Integer.parseInt(Acousticness) - 15;

        String DanceMax = AddZero(DMax);
        String DanceMin = AddZero(DMin);
        String EnergyMax = AddZero(EMax);
        String EnergyMin = AddZero(EMin);
        String AcousticMax = AddZero(AMax);
        String AcousticMin = AddZero(AMin);
        String bpmMax = AddZero(Max);
        String bpmMin = AddZero(Min);
        try {
            ResultSet results;
            int checkCount = 0;
            if (RecordsMain.getUI().BillboardCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().BillboardEDMCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().BillboardCountryCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().BillboardRnBCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().HypeCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().ShazamCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().ItunesCountryCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().ItunesRnBCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().ItunesEDMCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }
            if (RecordsMain.getUI().UKCheckbox.isSelected()) {
                checkCount = checkCount + 1;
            }

            if (checkCount != 0) {
                String NewSQL = "";
                String FinalSQL = "";

                for (int x = 0; x < checkCount; x++) {

                    if (RecordsMain.getUI().BillboardCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Billboard-Pop%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Billboard-Pop%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().BillboardEDMCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Billboard-EDM%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Billboard-EDM%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().BillboardCountryCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Billboard-Country%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Billboard-Country%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().BillboardRnBCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Billboard-RnB%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Billboard-RnB%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().HypeCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Hypem%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Hypem%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().ItunesCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Itunes-Pop%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Itunes-Pop%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().ItunesCountryCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Itunes-Country%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Itunes-Country%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().ItunesRnBCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Itunes-Rn%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Itunes-RnB%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().ItunesEDMCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Itunes-EDM%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Itunes-EDM%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().ShazamCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Shazam%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Shazam%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }

                    if (RecordsMain.getUI().ShazamCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Itunes-Pop%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Itunes-Pop%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                    if (RecordsMain.getUI().BeatportCheckbox.isSelected()) {
                        if (checkCount < 2 || x == 0) {
                            FinalSQL = "songs.Location LIKE '%Beatport%'";
                        }
                        if (checkCount > 1 && x > 0) {
                            NewSQL = " OR songs.Location LIKE '%Beatport%'";
                            FinalSQL = FinalSQL + NewSQL;
                        }
                    }
                }
                results = RecordsMain.dba.addLibChartsToPref(genre, bpmMax, bpmMin, FinalSQL, DanceMax, DanceMin, EnergyMax, EnergyMin, AcousticMax, AcousticMin);

            } else {
                //System.out.println("Default");
                results = RecordsMain.dba.defQuerey(genre, bpmMax, bpmMin, DanceMax, DanceMin, EnergyMax, EnergyMin, AcousticMax, AcousticMin);
            }
//
            DefaultTableModel model = (DefaultTableModel) RecordsMain.getUI().songPrefTable.getModel();

            // clear existing rows
            while (model.getRowCount() > 0) {
                model.removeRow(0);
                if (RecordsMain.getUI().jTabbedPane1.getSelectedIndex() == 1) {
                    RecordsMain.getUI().BasedOn.setVisible(false);
                    if (model.getRowCount() == 1) {
                        RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Song");
                    } else {
                        RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Songs");
                    }
                } else {
                    RecordsMain.getUI().BasedOn.setVisible(true);
                    DefaultTableModel model2 = (DefaultTableModel) RecordsMain.getUI().songPrefTable.getModel();
                    if (model.getRowCount() == 1) {
                        RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Song");
                    } else {
                        RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Songs");
                    }
                }
            }
            // While there are more results to process
            while (results.next()) {
                // Build a Vector of Strings for the table row
                List<String> data = new LinkedList<String>();
                data.add(results.getString("SONG_TITLE"));
                data.add(results.getString("ARTIST"));
                data.add(results.getString("ALBUM"));
                data.add(results.getString("GENRE"));
                data.add(results.getString("BPM"));
                data.add(results.getString("THE_KEY"));
                data.add(results.getString("LOCATION"));

                model.addRow(data.toArray());
                if (RecordsMain.getUI().jTabbedPane1.getSelectedIndex() == 1) {
                    RecordsMain.getUI().BasedOn.setVisible(false);
                    if (model.getRowCount() == 1) {
                        RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Song");
                    } else {
                        RecordsMain.getUI().chartSongNum.setText(model.getRowCount() + " Songs");
                    }
                } else {
                    RecordsMain.getUI().BasedOn.setVisible(true);
                    DefaultTableModel model2 = (DefaultTableModel) RecordsMain.getUI().songPrefTable.getModel();
                    if (model.getRowCount() == 1) {
                        RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Song");
                    } else {
                        RecordsMain.getUI().chartSongNum.setText(model2.getRowCount() + " Songs");
                    }
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            Logger.getLogger(RecTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String AddZero(int num) {
        System.out.println("RecTable.AddZero(" + num + ")");

        String numString = "";
        if (num < 10) {
            numString = "00" + num;
        } else if (num < 100 && num > 10) {
            numString = "0" + num;
        } else {
            numString = "" + num;
        }

        return numString;
    }
}
